package com.holystone.orca.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.holystone.orca.base.service.impl.BaseServiceImpl;
import com.holystone.orca.common.PageData;
import com.holystone.orca.sys.model.SysDepAllDataVO;
import com.holystone.orca.sys.model.SysDepartmentVO;
import com.holystone.orca.sys.mybatis.dao.SysDepartmentMapper;
import com.holystone.orca.sys.mybatis.model.SysDepartment;
import com.holystone.orca.sys.mybatis.model.SysStaff;
import com.holystone.orca.sys.service.ISysDepartmentService;
import com.holystone.orca.sys.service.ISysStaffService;
@Service("sysDepartmentService")
public class SysDepartmentServiceImple extends BaseServiceImpl<SysDepartment> 
	implements	ISysDepartmentService {

	private static Logger logger = Logger.getLogger(SysDepartmentServiceImple.class);
	@Autowired
	private SysDepartmentMapper sysDepartMapper;
	@Autowired
	private ISysStaffService sysStaffService;
	
	/**
	 * @desc:部门id获取部门信息
	 * @author: guanzhichao
	 * @date:2015年11月24日 下午2:09:53
	 * @Param: depId 部门id
	 * @return: List<SysDepartment>
	 */
	@Override
	public List<SysDepartment> selectDepartById(int depId) {
		logger.debug(">>> 部门id获取部门 SysDepartmentServiceImple.selectDepartById 参数：depId="+depId);
		List<SysDepartment> departList = new ArrayList<SysDepartment>();
		SysDepartment depart = sysDepartMapper.selectByPrimaryKey(depId);
		getDeparts(depId,departList);
		departList.add(depart);
		logger.debug("<<<部门id获取部门 SysDepartmentServiceImple.selectDepartById");
		return departList;
	}

	
	/**
	 * @desc: 获取部门树信息
	 * @author: guanzhichao
	 * @date: 2015年11月24日 
	 * @Param: depId 部门id
	 * @Param: departList 部门集合
	 * @return: List<SysDepartment>
	 */
	private void getDeparts(int depId, List<SysDepartment> departList) {
		logger.debug(">>> 获取部门树信息 SysDepartmentServiceImple.getDeparts 参数：depId="+depId +" 部门长度：departList="+departList.size());
		
		SysDepartment depart = new SysDepartment();
		depart.setDepPid(depId);
		List<SysDepartment> sysDepList = sysDepartMapper.select(depart);
		if(sysDepList.size() != 0){
			departList.addAll(sysDepList);
			depId = sysDepList.get(0).getDepId();
			getDeparts(depId, departList);
		}
		//logger.debug(">>>获取部门树信息 SysDepartmentServiceImple.getDeparts 树长度：departList="+departList.size());
	}

	
	/**
	 * @desc: 根据部门id， 实例id查询设备
	 * @author: guanzhichao
	 * @date: 2015年11月24日 
	 * @Param: depIds
	 * @Param: insIds
	 * @return: List<SysDepartmentVO>
	 */
	@Override
	public List<SysDepartmentVO> findBundEqemet(SysDepartmentVO deptVO) {
		List<SysDepartmentVO> departList = sysDepartMapper.findBundEqemet(deptVO);
		return departList;
	}
	
	/**
	 * @desc:通过部门id获取它本身和它所有的子孙对象 
	 * @author: 宋旭源
	 * @date: 2015年11月30日 下午7:59:37
	 * @param depId
	 * @return List<SysDepAllDataVO>
	 */
	@Override
	public List<SysDepAllDataVO> selectDepChildrensById(Integer depId,String whereStr){
		return sysDepartMapper.selectDepChildrensById("getDepChildLst("+depId+")",whereStr);
	}
	
	/* (non-Javadoc)
	 * @see com.holystone.orca.sys.service.ISysDepartmentService#selectDepChildrensByIdList(java.util.List, java.lang.String)
	 */
	@Override
	public List<SysDepAllDataVO> selectDepChildrensByIdList(List<Integer> depIds,String whereStr){
		StringBuffer funcBuffer = new StringBuffer();
		funcBuffer.append("CONCAT(");
		for (int i = 0; i < depIds.size(); i++) {
			if (i != depIds.size()-1) {
				funcBuffer.append("getDepChildLst("+depIds.get(i)+"),',',");
			}else {
				funcBuffer.append("getDepChildLst("+depIds.get(i)+")");
			}
		}
		funcBuffer.append(")");
		List<SysDepAllDataVO> sysDepAllDataVOs = sysDepartMapper.selectDepChildrensById(funcBuffer.toString(),whereStr);
		return sysDepAllDataVOs;
	}


	@Override
	public List<Integer> selectDepChildrenIdsById(Integer depId, String whereStr) {
		return sysDepartMapper.selectDepChildrenIdsById("getDepChildLst("+depId+")",whereStr);
	}


	@Override
	public List<Integer> selectDepChildrenIdsByIdList(List<Integer> depIds,
			String whereStr) {
		StringBuffer funcBuffer = new StringBuffer();
		funcBuffer.append("CONCAT(");
		for (int i = 0; i < depIds.size(); i++) {
			if (i != depIds.size()-1) {
				funcBuffer.append("getDepChildLst("+depIds.get(i)+"),',',");
			}else {
				funcBuffer.append("getDepChildLst("+depIds.get(i)+")");
			}
		}
		funcBuffer.append(")");
		List<Integer> sysDepAllDataVOs = sysDepartMapper.selectDepChildrenIdsById(funcBuffer.toString(),whereStr);
		return sysDepAllDataVOs;
	}

	//用户id获取用户所在部门
	@Override
	public List<SysDepartment> selectDepartByUserId(String userId)
			throws Exception {
		logger.debug(">>> 用户id获取用户所在部门 SysDepartmentServiceImple.selectDepartByPersonId:参数 userId="+userId);
		SysStaff staff = sysStaffService.selectByKey(Integer.valueOf(userId));
		int depId = staff.getDepId();
		List<SysDepartment> depList = selectDepartById(depId);
		logger.debug("<<< 用户id获取用户所在部门 SysDepartmentServiceImple.selectDepartByPersonId 部门长度：depList="+depList.size());
		return depList;
	}


	@Override
	public List<SysDepartment> selectDeptName() throws Exception {
		List<SysDepartment> list=sysDepartMapper.selectDeptName();
		return list;
	}


	@Override
	public List<SysDepartment> selectParentDep(Map<Object, String> map) throws Exception {
		List<SysDepartment> list=sysDepartMapper.selectParentDep(map);
		return list;
	}


	@Override
	public PageData pagingSysDep(int iPageNo, int iPageSize, String orderField,
			String depName) throws Exception {
		Map<Object, String> map=new HashMap<Object, String>();
		map.put("depName", depName);
		
        //分页查询
        PageHelper.startPage(iPageNo, iPageSize);
        PageInfo<SysDepartment> pageInfo = new PageInfo<SysDepartment>(sysDepartMapper.selectParentDep(map));
		PageData pageData =  new PageData();
		pageData.setPageList(pageInfo.getList());
		pageData.setTotalCount(pageInfo.getTotal());
		pageData.setPageSize(pageInfo.getPageSize());
		pageData.setPageNo(pageInfo.getPageNum());
		pageData.setTotalPage(pageInfo.getPages());
		return pageData;
	}


	@Override
	public boolean addDep(Map<Object,String> map) throws Exception {
		int result=sysDepartMapper.addDep(map);
		if(result==1){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public List<SysDepartment> selectIsHaveDep(Map<Object,String> map)
			throws Exception {
		List<SysDepartment> sysDepartment=sysDepartMapper.selectIsHaveDep(map);
		return sysDepartment;
	}


	@Override
	public SysDepartment selectDepBydepId(int depId) throws Exception {
		SysDepartment sysDepartment=sysDepartMapper.selectDepBydepId(depId);
		return sysDepartment;
	}


	@Override
	public boolean editDepBydepId(Map<String, String> map) throws Exception {
		int result=sysDepartMapper.editDepBydepId(map);
		if(result==1){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public PageData pagingSysChildDep(int iPageNo, int iPageSize,
			String orderField,String depName, String depId) throws Exception {
		Map<Object, String> map=new HashMap<Object, String>();
		map.put("depId", depId);
		map.put("depName", depName);
		
        //分页查询
        PageHelper.startPage(iPageNo, iPageSize);
        PageInfo<SysDepartment> pageInfo = new PageInfo<SysDepartment>(sysDepartMapper.selectChildDep(map));
		PageData pageData =  new PageData();
		pageData.setPageList(pageInfo.getList());
		pageData.setTotalCount(pageInfo.getTotal());
		pageData.setPageSize(pageInfo.getPageSize());
		pageData.setPageNo(pageInfo.getPageNum());
		pageData.setTotalPage(pageInfo.getPages());
		return pageData;
	}


	@Override
	public List<SysDepartment> selectDepNoSelfBydepId(Map<Object,String> map)
			throws Exception {
		List<SysDepartment> list = sysDepartMapper.selectDepNoSelfBydepId(map);
		return list;
	}


	@Override
	public boolean delDepByDepId(int depId) throws Exception {
		int result=sysDepartMapper.delDepByDepId(depId);
		if(result!=0){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public boolean delSelectDep(List<Integer> list) throws Exception {
		int result=sysDepartMapper.delSelectDep(list);
		if(result!=0){
			return true;
		}else{
			return false;
		}
	}
	
}
